<?php
namespace common\models;

use Yii;
use yii\db\ActiveRecord;
use common\models\Superstar;
use common\models\Type;
use yii\web\Link;
use yii\web\Linkable;
use yii\helpers\Url;



class Article extends ActiveRecord implements Linkable{

  public function getSsName()
  {
      return $this->hasOne(Superstar::className(), ['ss_id' => 'ss_id']);
  }
  public function getType()
  {
      return $this->hasOne(Type::className(), ['type_id' => 'type_id']);
  }

  //提交处理
  public function subHandle($id){
    $atcl = Article::findOne($id);
    $atcl->atcl_handled = 1;
    return $atcl->save();
  }

  public function fields() {
    return [
      'id'=>'atcl_id',
      'url'=>'atcl_url',
      'title'=>'atcl_title',
      'content'=>'atcl_body',
      'aspects'=>function($model)
        {
          if($model->atcl_negative_prob > 0.75){
            return '负面';
          }else if($model->atcl_negative_prob > 0.25 && $model->atcl_negative_prob < 0.75){
            return '中性';
          }else{
            return '正面';
          }
        },
      'superstar'=>function ($model)
      {
        return $model->ssName->ss_name;
      },
      'from'=>function ($model)
      {
        return $model->type->type_name;
      },
      'color'=>function($model)
      {
        if($model->atcl_negative_prob > 0.75){
          return '#f0090f';
        }else if($model->atcl_negative_prob > 0.25 && $model->atcl_negative_prob < 0.75){
          return '#95bb4a';
        }else{
          return '#2e8ebe';
        }
      },
      'handle'=>function($model)
      {
        if($model->atcl_handled == 0 ){
          return '未处理';
        }else if($model->atcl_handled == 1 ){
          return '处理中';
        }else{
          return '已处理';
        }
      },
      'date'=>function($model)
      {
        return substr($model->atcl_spiderdate,0,10);
      }
    ];
  }

  public function getLinks(){
      return [
        Link::REL_SELF => Url::to(['article/view','id'=>$this->atcl_id],true),  
          'edit'=>Url::to(['article/update','id'=>$this->atcl_id],true),
          'index'=>Url::to(['articles'],true),
      ];
  }

  public function getContentByID($id){
    return Article::find()
      ->where(['=','atcl_id',$id])
      ->asArray()
      ->one();
  }
}